<?php
/*
 * @Author: lokei
 * @Date: 2022-08-16 13:16:33
 * @LastEditors: lokei
 * @LastEditTime: 2023-04-10 20:31:35
 * @Description: 
 */

namespace App\Http\Controllers\Pay\Alipay;

use App\Common\Tools\ResultTool;
use App\Common\Tools\Sn;
use App\Http\Controllers\Controller;
use App\Models\Pay\Record\PayRecordModel;
use App\Models\Platform\ConfModel as PlatformConfModel;
use Illuminate\Http\Request;

use Alipay\EasySDK\Kernel\Factory;
use App\Common\Alipay\Common;

class AppController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    //
    public function pay(Request $request)
    {
        $user = app('auth')->user();
        $sn = Sn::build_order_no();
        $pay_record = new PayRecordModel();
        $pay_record->uid = $user->uid;
        $pay_record->mode = $request->input('mode');
        $pay_record->pay_mode = '8';
        $pay_record->tid = $sn;
        $pay_record->order_id = $request->input('order_id');
        $pay_record->money = $request->input('money');
        $pay_record->status = '0';
        $pay_record->save();
        $pay_description = '';
        if ($request->input('description') != null && $request->input('description') != '') {
            $pay_description = $request->input('description');
        } else {
            $platform_conf = PlatformConfModel::first();
            $pay_description = $platform_conf->name . '支付';
        }

        $options = Common::getOptions();
        $options->notifyUrl = env('HOST_URL') . ((env('HOST_PORT') != null && intval(env('HOST_PORT')) > 0) ? (':' . env('HOST_PORT')) : '') . '/api/pay/alipay/notify' . ((config('app.proj_identity') != null && config('app.proj_identity') != '') ? ('/' . config('app.proj_identity')) : '');
        Factory::setOptions($options);
        if (config('app.proj_identity') != null && config('app.proj_identity') != '') {
            $result = Factory::payment()->app()->agent(config('app.app_auth_token'))->pay($pay_description, $sn, $request->input('money'), $request->input('openid'))->toMap();
        } else {
            $result = Factory::payment()->app()->pay($pay_description, $sn, $request->input('money'), $request->input('openid'))->toMap();
        }
        $res = ResultTool::success();
        $res['data']['orderStr'] = $result['body'];
        return $res;
    }
}
